<?php
// +----------------------------------------------------------------------
// | TOPThink [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://topthink.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace Think\Log\Driver;

class Elk
{
    /**
     * 项目名称
     *
     * @var string
     */
    protected $project_name = '';

    /**
     * 配置信息
     *
     * @var array
     */
    protected $config = array(
        'log_time_format' => ' c ',
        'log_file_size'   => 2097152,
        'log_path'        => '/data/logs/thinkphp/',
    );

    /**
     * 实例化并传入参数
     *
     * @param  array $config 日志配置文件
     *
     * @author cliu <liuchangtao@clcw.com.cn>
     */
    public function __construct($config = array())
    {
        $this->project_name       = C('PROJECT_NAME') ?: 'default';
        $this->config             = array_merge($this->config, $config);
    }

    /**
     * 日志写入接口
     *
     * @access public
     * @param string $log          日志信息
     * @param string $destination  写入目标
     *
     * @return void
     */
    public function write($log, $destination = '')
    {
        $now = date($this->config['log_time_format']);
        if (empty($destination)) {
            $destination = $this->config['log_path'] . $this->project_name . '_' . MODULE_NAME . '_' . date('y_m_d') . '.log';
        }

        // 把以模型文件夹转换成日志文件前缀
        $pos = stripos($destination, MODULE_NAME);
        if ($pos) {
            $destination = 'tp_'. $this->project_name . '_' . MODULE_NAME . '_' . substr($destination, strripos($destination, '/')+1);
        }

        $destination = $this->config['log_path'] . $destination;

        // 自动创建日志目录
        $log_dir = dirname($destination);
        if (!is_dir($log_dir)) {
            mkdir($log_dir, 0755, true);
        }
        // 检测日志文件大小，超过配置大小则备份日志文件重新生成
        if (is_file($destination) && floor($this->config['log_file_size']) <= filesize($destination)) {
            rename($destination, dirname($destination) . '/' . time() . '-' . basename($destination));
        }

        $log_array = explode(':', $log, 2);

        $log_content = array(
            'project'     => $this->project_name,
            'datetime'    => trim($now),
            'remote_addr' => (!$_SERVER['REMOTE_ADDR']?'-':$_SERVER['REMOTE_ADDR']),
            'domain'      => (!$_SERVER['HTTP_HOST']?'-':$_SERVER['HTTP_HOST']),
            'request'     => (!$_SERVER['REQUEST_URI']?'-':$_SERVER['REQUEST_URI']),
            'level'       => $log_array[0],
            'message'     => trim($log_array[1]),
        );

        $log_json = json_encode($log_content);
        
        // echo $destination;
        error_log($log_json . "\r\n", 3, $destination);
    }

    /**
     * 业务日志输出
     * @access public
     * @param string $logArr       日志信息,要求1维数组格式
     * @param string $destination  写入目标
     *
     * @return void
     */
    public function busiWrite($logArr, $destination = ''){
        $now = date($this->config['log_time_format']);
        if (empty($destination)) {
            $destination = $this->config['log_path'] . $this->project_name . '_' . MODULE_NAME . '_' . date('y_m_d') . '.log';
        }

        // 把以模型文件夹转换成日志文件前缀
        $pos = stripos($destination, MODULE_NAME);
        if ($pos) {
            $destination = 'bs_'. $this->project_name . '_' . MODULE_NAME . '_' .
                           substr($destination, strripos($destination, '/')+1);
        }

        $destination = $this->config['log_path'] . $destination;
        
        // 自动创建日志目录
        $log_dir = dirname($destination);
        if (!is_dir($log_dir)) {
            mkdir($log_dir, 0755, true);
        }
        // 检测日志文件大小，超过配置大小则备份日志文件重新生成
        if (is_file($destination) && floor($this->config['log_file_size']) <= filesize($destination)) {
            rename($destination, dirname($destination) . '/' . time() . '-' . basename($destination));
        }

        $log_json = json_encode($logArr);

        // echo $destination;
        error_log($log_json . "\r\n", 3, $destination);

    }
}
